#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <errno.h>
#include <fcntl.h>
#include <string>
using namespace std;
int main()
{
    //设置0号文件描述符是非阻塞状态
    int flag = fcntl(0,F_GETFL);
    fcntl(0,F_SETFL,flag | O_NONBLOCK);
    //创建句柄的接口
    int epfd = epoll_create(5);
    if(epfd < 0)
    {
        perror("create");
        return 0;
    }
    //操作0号接口
    struct epoll_event ee;
    ee.data.fd = 0;
    ee.events = EPOLLIN | EPOLLET;
    epoll_ctl(epfd,EPOLL_CTL_ADD,0,&ee);
    while(1)
    {

        //获得就绪的事件结构
        struct epoll_event arr[10];
        int ret = epoll_wait(epfd,arr,10,-1);
        if(ret < 0)
        {
            perror("wait");
            return 0;
        }
        else if(ret == 0)
        {
            printf("timeout\n");
            continue;
        }
        //装储buf的全部数据
        string str;
        //读数据
        while(1)
        {
            char buf[2] = {0};
            int ret = read(arr[0].data.fd,buf,sizeof(buf)-1);
            if(ret < 0)
            {
                if(errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    break;
                }
            }
            str.append(buf);            
            printf("%s ",buf);
        }
        printf("str:%s\n",str.c_str());
    }
    return 0;
}
